package iCalImport;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JFileChooser;

import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import todo.MyTask;
import calendar.ImportEvent;

/**
 * Importer für iCal
 * 
 * @author Michi
 * 
 */
public class Import {

	/**
	 * Liefert alle Termine des Files.
	 * 
	 * @param args
	 */

	public static ArrayList<ImportEvent> getTermine(File f) {

		ArrayList<ImportEvent> liste = new ArrayList<ImportEvent>();

		FileInputStream fin;

		// Create a file chooser
		@SuppressWarnings("unused")
		final JFileChooser fc = new JFileChooser();

		// //In response to a button click:
		// int returnVal = fc.showOpenDialog(aComponent);
		//
		try {

			// fin = new FileInputStream("Test.ics");
			fin = new FileInputStream(f);

			CalendarBuilder builder = new CalendarBuilder();

			Calendar calendar = builder.build(fin);

			// ComponentList liste = calendar.getComponents();
			// Iterator<Component> itr = liste.iterator();
			//
			// while (itr.hasNext()) {
			// Component component = itr.next();
			//
			// Property pro = component.getProperty("DTSTART");
			// if (pro != null) {
			// System.out.println("Feiertag beginnt am: " + pro.getValue());
			// }
			//
			// }

			for (@SuppressWarnings("rawtypes")
			Iterator i = calendar.getComponents().iterator(); i.hasNext();) {

				Component component = (Component) i.next();
				System.out.println("Component [" + component.getName() + "]");

				// /////////////////// WENN DIE KOMPONENTE EIN TERMIN IST
				// /////////////////////

				if (component.getName() == "VEVENT") {

					String name = null;
					Date ende = null;
					Date start = null;
					String location = "";
					String description = "";

					for (@SuppressWarnings("rawtypes")
					Iterator j = component.getProperties().iterator(); j
							.hasNext();) {

						Property property = (Property) j.next();
						System.out.println("Property [" + property.getName()
								+ ", " + property.getValue() + "]");
						if (property.getName() == "SUMMARY") {
							name = property.getValue();
						}

						if (property.getName() == "DTEND") {
							ende = formDate(property.getValue());
						}

						if (property.getName() == "DTSTART") {
							start = formDate(property.getValue());

						}
						if (property.getName() == "LOCATION") {
							location = property.getValue();
						}
						if (property.getName() == "DESCRIPTION") {
							description = property.getValue();
						}

					}
					// Wenn Name Start und Ende definiert sind:
					if (name != null && start != null && ende != null) {
						ImportEvent evt = new ImportEvent(name, start, ende,
								location, description);
						liste.add(evt);
					}

				}

			}

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (ParserException e3) {
			e3.printStackTrace();
		}
		return liste;

	}

	/**
	 * Liefert alle Aufgaben des Files
	 */

	@SuppressWarnings("rawtypes")
	public static ArrayList<MyTask> getAufgaben(File f) {

		ArrayList<MyTask> liste = new ArrayList<MyTask>();

		FileInputStream fin;

		// Create a file chooser
		@SuppressWarnings("unused")
		final JFileChooser fc = new JFileChooser();

		// //In response to a button click:
		// int returnVal = fc.showOpenDialog(aComponent);
		//
		try {

			// fin = new FileInputStream("Test.ics");
			fin = new FileInputStream(f);

			CalendarBuilder builder = new CalendarBuilder();

			Calendar calendar = builder.build(fin);

			for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {

				Component component = (Component) i.next();
				System.out.println("Component [" + component.getName() + "]");

				// /////////////////// WENN DIE KOMPONENTE EINE AUFGABE IST
				// /////////////////////

				if (component.getName() == "VTODO") {

					String name = null;
					Date deadline = null;
					int priority = 1;
					boolean done = false;
					String description = "";

					for (Iterator j = component.getProperties().iterator(); j
							.hasNext();) {

						Property property = (Property) j.next();
						System.out.println("Property [" + property.getName()
								+ ", " + property.getValue() + "]");
						if (property.getName() == "SUMMARY") {
							name = property.getValue();
						}

						if (property.getName() == "DUE") {
							deadline = formDate(property.getValue());
						}

						if (property.getName() == "PRIORITY") {
							if (Integer.parseInt(property.getValue()) == 9) {
								priority = 0;
							} else if (Integer.parseInt(property.getValue()) == 5) {
								priority = 1;
							} else {
								priority = 2;
							}
						}

						if (property.getName() == "DESCRIPTION") {
							description = property.getValue();
						}
						if (property.getName() == "COMPLETED") {
							done = true;
						}

					}
					// Wenn Name definiert ist:
					if (name != null) {
						MyTask evt = new MyTask(name, deadline, null, priority,
								description, 0, done);

						liste.add(evt);
					}

				}

			}

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (ParserException e3) {
			e3.printStackTrace();
		}
		return liste;

	}

	/**
	 * Wandelt String in Date
	 * 
	 * @param s
	 *            String der gewandelt werden soll
	 * @return Datuma us String
	 */
	@SuppressWarnings("deprecation")
	public static Date formDate(String s) {

		// 20121003T113000

		char[] cs = s.toCharArray();

		int jahr = (Integer.parseInt("" + cs[0] + cs[1] + cs[2] + cs[3])) - 1900;
		int monat = (Integer.parseInt("" + cs[4] + cs[5] + "")) - 1;
		int tag = Integer.parseInt("" + cs[6] + cs[7] + "");
		int stunden = 0;
		int minuten = 0;
		// Wenn Datum mit Uhrzeit:
		if (cs.length > 8) {
			stunden = Integer.parseInt("" + cs[9] + cs[10]);
			minuten = Integer.parseInt("" + cs[11] + cs[12]);

		}

		return new Date(jahr, monat, tag, stunden, minuten);
		// [DTEND, 20130306]

	}

}
